﻿using Common.Library;
using ImageResizer;
using ImageResizer.Configuration;
using Microsoft.Phone.Shell;
using OnlineVideos.Common;
using OnlineVideos.Data;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Xml.Linq;

namespace OnlineVideos.UI
{
 public static class LiveTileUpDate
    {
     
    

     public static void secondaryTileUpDate()
     {
         //ShellTile oTile = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("flip".ToString()));
   

         //if (oTile != null && oTile.NavigationUri.ToString().Contains("flip"))
         //{
         ShellTile primaryTile = ShellTile.ActiveTiles.First();
        
             List<ShowList> downloadrecentlist = null;
             int downloadHistory;
             downloadrecentlist = OnlineShow.GetRecentlyAddedShows();

             downloadHistory = downloadrecentlist.Count();

             if (Storage.FileExists(ResourceHelper.ProjectName + "/History.xml"))
             {
                 XDocument xdoc = Storage.ReadFileAsDocument(ResourceHelper.ProjectName + "/History.xml");

                 var data = from c in xdoc.Descendants("History") select c;

                 SyncAgentState.HistoryCount = data.Count();
                 AppSettings.LiveTileHistroryCount = data.Count();
             }
             else
             {
                 SyncAgentState.HistoryCount = downloadHistory;
                 AppSettings.LiveTileHistroryCount = downloadHistory;
             }
             FlipTileData oFliptile = new FlipTileData();
             foreach (var item in downloadrecentlist)
             {
                 oFliptile.Title = ResourceHelper.ProjectName;
                 oFliptile.Count = AppSettings.LiveTileHistroryCount;
                 oFliptile.BackTitle = item.Title;
                 oFliptile.SmallBackgroundImage = new Uri("Assets/Tiles/Flip/logo159x159.png", UriKind.Relative);
                 oFliptile.BackgroundImage = new Uri("Assets/Tiles/Flip/logo336x336.png", UriKind.Relative);
                 oFliptile.WideBackgroundImage = new Uri("Assets/Tiles/Flip/logo691x336.png", UriKind.Relative);
                  string ImageUrl = ResourceHelper.GetLiveTileUrl(item.TileImage);
                  oFliptile.BackBackgroundImage = new Uri(ImageUrl,UriKind.RelativeOrAbsolute); //new Uri("/Assets/Tiles/336-336/3.jpg", UriKind.Relative);
                 oFliptile.WideBackBackgroundImage = new Uri(ImageUrl, UriKind.RelativeOrAbsolute);// new Uri("/Assets/Tiles/336-336/3.jpg", UriKind.Relative);
                 primaryTile.Update(oFliptile);
             }
             AppSettings.LiveTileBackgroundAgentStatus = true;
     }

   

     //public static void ResizeImage(ImageSource biInput, string imgname,string Path,int wdith,int height)
     //{
     //    try
     //    {
     //        IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
     //        string filePath = string.Empty;
     //        WriteableBitmap wbOutput;
     //        Image imgTemp = new Image();
     //        imgTemp.Source = biInput;

     //        wbOutput = new WriteableBitmap(imgTemp, null);
     //        using (MemoryStream stream = new MemoryStream())
     //        {
     //            wbOutput.SaveJpeg(stream, wdith, height, 0, 100);

     //            filePath = imgname;
     //            if (!Storage.DirectoryExists(Path))
     //                Storage.CreateDirectory(Path.Substring(0, Path.LastIndexOf('/')));
     //            using (IsolatedStorageFileStream local = isoStore.CreateFile(Path + filePath))
     //            {
     //                local.Write(stream.GetBuffer(), 0, stream.GetBuffer().Length);
     //            }
     //        }
     //        Exceptions.UpdateAgentLog("ResizeImage Completed In PrimaryTileUpdate");

     //    }
     //    catch (Exception ex)
     //    {
     //        Exceptions.SaveOrSendExceptions("Exception in ResizeImage Method In Vidoes file", ex);
     //    }
     //}
        

     

     }
    
}
